﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;
using System.Data.SqlClient;
using System.Data;

namespace xTeam.Hengle.DataAccess
{
    public class ProductDataAccess
    {
        public ProductDataAccess(string connectionString)
        {
            SQLProcedureHelper.ConnectionString = connectionString;
        }


        public int SaveBase(EProduct prdouct)
        {
            int result = 0;
            SqlParameter[] parameters = 
            {
                new SqlParameter("@productname",SqlDbType.NVarChar,255){Value=prdouct.ProductName},
                new SqlParameter("@description",SqlDbType.NVarChar,255){Value=prdouct.Description},
                new SqlParameter("@keywords",SqlDbType.NVarChar,255){Value=prdouct.KeyWords},
                new SqlParameter("@productno",SqlDbType.NVarChar,20){Value=prdouct.ProductNo},
                new SqlParameter("@classid",SqlDbType.Int){Value=prdouct.ClassId},
                new SqlParameter("@ProductId",SqlDbType.Int){Value=prdouct.ProductId}
            };

            result = Convert.ToInt32(SQLProcedureHelper.ExecuteScalar("product_update", parameters));
            return result;
        }

        public List<EProduct> Search(int pageSize, int pageIndx, int classId, string key, ref int pageCount)
        {
            List<EProduct> productList = new List<EProduct>();

            SqlParameter[] parameter = 
            {
                new SqlParameter("@classid",SqlDbType.Int){Value=classId},
                new SqlParameter("@PageIndex",SqlDbType.Int){Value=pageIndx},
                new SqlParameter("@PageSize",SqlDbType.Int){Value=pageSize}
            };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("product_selectList", parameter);
            while (reader.Read())
            {
                pageCount = Convert.ToInt32(reader["PageCount"]);
                productList.Add(new EProduct()
                {
                    ProductId = Convert.ToInt32(reader["ProductId"]),
                    Description = reader["Description"].ToString(),
                    KeyWords = reader["KeyWords"].ToString(),
                    ProductName = reader["ProductName"].ToString(),
                    ProductNo = reader["ProductNo"].ToString(),
                    AddDateTime = Convert.ToDateTime(reader["AddDateTime"]),
                    ImageName = reader["ImageName"].ToString()
                });
            };
            reader.Close();

            return productList;
        }

        public EProduct GetBaseById(int productId)
        {
            EProduct product = null;

            SqlParameter[] parameter = { new SqlParameter("@ProductId", SqlDbType.Int) { Value = productId } };

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("product_select", parameter);
            if (reader.HasRows)
            {
                product = new EProduct();

                while (reader.Read())
                {
                    product.ClassId = Convert.ToInt32(reader["ClassId"]);
                    product.ProductId = Convert.ToInt32(reader["ProductId"]);
                    product.Description = reader["Description"].ToString();
                    product.ProductNo = reader["ProductNo"].ToString();
                    product.KeyWords = reader["KeyWords"].ToString();
                    product.ProductName = reader["ProductName"].ToString();
                    product.ImageName = reader["ImageName"].ToString();
                }
            }
            reader.Close();
            return product;
        }

        public EProductDetails GetDeatails(int productid)
        {
            EProductDetails details = new EProductDetails()
            {
                Services = string.Empty,
                PackList = string.Empty,
                FullSnap = string.Empty,
                Detail = string.Empty,
                Description = string.Empty,
                ProductId = productid
            };

            SqlParameter[] parameter = 
            {
                new SqlParameter("@productid",SqlDbType.Int){Value=productid}
            };

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("Product_Details_Select", parameter);
            if (reader.HasRows)
            {
                reader.Read();
                details.Detail = reader["Detail"].ToString();
                details.FullSnap = reader["FullSnap"].ToString();
                details.PackList = reader["PackList"].ToString();
                details.Services = reader["Services"].ToString();
                details.Description = reader["Description"].ToString();
            }

            return details;
        }

        public int SaveDetails(EProductDetails details)
        {
            int result = 0;
            SqlParameter[] parameter = 
            {
                new SqlParameter("@Services",SqlDbType.NVarChar,50){Value=details.Services},
                new SqlParameter("@Details",SqlDbType.NVarChar,3500){Value=details.Detail},
                new SqlParameter("@ProductId",SqlDbType.Int){Value = details.ProductId}

            };

            result = SQLProcedureHelper.ExecuteNonQuery("Product_Details_Save", parameter);

            return result;
        }

        public int SaveImage(EProductImage image)
        {
            int result = 0;
            SqlParameter[] parameters = 
            {
               new SqlParameter("@ImageId",SqlDbType.Int),
               new SqlParameter("@ImageName",SqlDbType.NVarChar,50),
               new SqlParameter("@Title",SqlDbType.NVarChar,255),
               new SqlParameter("@Description",SqlDbType.NVarChar,1000),
               new SqlParameter("@IsCover",SqlDbType.Bit),
               new SqlParameter("@ProductId",SqlDbType.Int),
               new SqlParameter("@Sequence",SqlDbType.Int)
            };

            parameters[0].Value = image.ImageId;
            parameters[1].Value = image.ImageName;
            parameters[2].Value = image.Title;
            parameters[3].Value = image.Description;
            parameters[4].Value = image.IsCover;
            parameters[5].Value = image.ProductId;
            parameters[6].Value = image.Sequence;

            result = SQLProcedureHelper.ExecuteNonQuery("Product_Image_Save", parameters);

            return result;
        }

        public int SetCover(int imageId)
        {
            int result = 0;
            SqlParameter[] parameters =
            {
                new SqlParameter("@ImageId",SqlDbType.Int)
            };
            parameters[0].Value = imageId;
            result = SQLProcedureHelper.ExecuteNonQuery("Product_Image_SetCover", parameters);
            return result;
        }

        public List<EProductImage> GetImageList(int productId)
        {
            List<EProductImage> imageList = new List<EProductImage>();

            SqlParameter[] parameters = 
            {
                new SqlParameter("@ProductId",SqlDbType.Int)
            };
            parameters[0].Value = productId;

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("Product_Image_List", parameters);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    imageList.Add(new EProductImage()
                    {
                        Description = reader["Description"].ToString(),
                        ImageId = Convert.ToInt32(reader["ImageId"]),
                        ImageName = reader["ImageName"].ToString(),
                        IsCover = Convert.ToBoolean(reader["IsCover"]),
                        ProductId = Convert.ToInt32(reader["ProductId"]),
                        Sequence = Convert.ToInt32(reader["Sequence"]),
                        Title = reader["Title"].ToString()
                    });
                }
            }
            reader.Close();

            return imageList;
        }

        public List<EProductImage> DeleteImage(string[] ids)
        {
            List<EProductImage> imageList = new List<EProductImage>();
            foreach (var item in ids)
            {
                SqlParameter[] parameters = 
                {
                   new  SqlParameter("@ImageId",SqlDbType.Int)
                };
                parameters[0].Value = item;
                SqlDataReader reader = SQLProcedureHelper.ExecuteReader("Product_Image_Delete", parameters);
                while (reader.Read())
                {
                    imageList.Add(new EProductImage()
                    {
                        ImageName = reader["ImageName"].ToString(),
                        ProductId = Convert.ToInt32(reader["ProductId"])
                    });
                }
                reader.Close();
            }
            return imageList;
        }

        public int UpdateImageDescription(int imageId, string title, string description, int sequence)
        {
            return SQLProcedureHelper.ExecuteNonQuery("Product_Image_UpdateDescription",
                new SqlParameter[] {
                    new SqlParameter("@ImageId",SqlDbType.Int){Value=imageId}
                    ,new SqlParameter("@Title",SqlDbType.NVarChar,50){Value=title}
                    ,new SqlParameter("@Description",SqlDbType.NVarChar,255){Value= description}
                    ,new SqlParameter("@Sequence",SqlDbType.Int){Value=sequence} });
        }

        public int Delete(int productId)
        {
            return SQLProcedureHelper.ExecuteNonQuery("product_delete"
                    , new SqlParameter[]{
                    new SqlParameter("@ProductId",SqlDbType.Int){Value = productId}});
        }
    }
}
